Overview

The application you are developing only has one screen so far. However, more screens may be required as more features are implemented. It’s important to set up the navigation hierarchy of the application as well as the different navigation rules to navigate from one screen to another. This is done by using a combination of navigators, such as Stacks, Tabs, Drawers or Modals. In this exercise, you will set up the root navigator of the Little Lemon food ordering app.

Scenario

So far, you created the onboarding screen that lets the user register with their first name, last name and email address. However, some questions remain unanswered:

Prerequisites

To complete this exercise, you should have created the onboarding flow to collect basic user information such as a first name, last name and an email address.

Application screens

Before implementing navigation, let’s explore the various destinations required for the Little Lemon food ordering app. There should be three navigation destinations in your app, each represented by a Composable. These are the:

Users should be able to navigate between these destinations in your app. In more detail, the requirements for the navigation flow are as follows:

The following navigation diagram illustrates these requirements:

Instructions

Step 1: Dependency

Step 2: Create empty screens

Step 3: Create the destinations

Tip: It is common practice to use the same name for the route as the Destination name, for example, Home .

Step 4: Create the Navigation Composable

Tip: navController can be created by using the rememberNavController() delegate.

Step 5: Update the onboarding navigation flow

Tip: To implement registration validation behavior, leverage the onClick from the Button Composable.

Tip: To validate Composable text, use th e isBlank method, for example, firstName.isBlank()

As you need to navigate from the Onboarding screen to the Home screen, it is required to have the navController parameter in the Onboarding Composable to call the navigate function. To do this, add the navController parameter of the NavHostContoller type to the Onboarding screen Composable in the Onboarding.kt file. Then call the navController.navigate() function when the user input is valid.

Conclusion

In this exercise, you created an authentication flow with Jetpack Navigation to conditionally render specific screens, depending on whether the user has completed the onboarding phase or not. Furthermore, you persisted information in the disk using SharedPreferences so that when you start up the application, the previous state of the user journey across the app is remembered.